package org.csu.mypetstore.persistence.impl;

import org.csu.mypetstore.domain.CartList;
import org.csu.mypetstore.persistence.CartListDAO;
import org.csu.mypetstore.persistence.DBUtil;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartListDAOImpl implements CartListDAO {

    private static final String INSERT = "INSERT INTO cart (username,itemid,productid,Description,quantity,inStock,total) VALUES (?,?,?,?,?,?,?)";
    private static final String DELETE = "DELETE  FROM cart WHERE username = ?";
    private static final String GET = "SELECT * FROM cart WHERE username = ?";
    private static final String DELETE_ITEM = "DELETE FROM cart WHERE username=? AND itemid=?";

    @Override
    public void deleteCartItemByUsernameAndItemId(String username, String itemid) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ITEM);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,itemid);
            preparedStatement.executeUpdate();

            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void insertCartList(CartList cartList) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT);
            preparedStatement.setString(1,cartList.getUsername());
            preparedStatement.setString(2,cartList.getItemid());
            preparedStatement.setString(3,cartList.getProductid());
            preparedStatement.setString(4,cartList.getDescription());
            preparedStatement.setInt(5,cartList.getQuantity());
            preparedStatement.setString(6,String.valueOf(cartList.getInStock()));
            preparedStatement.setBigDecimal(7,cartList.getTotal());
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteCartByUsername(String username) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(DELETE);
            preparedStatement.setString(1,username);
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<CartList> getCartListByUsername(String username) {
        List<CartList> cartLists = null;
        try {
            cartLists = new ArrayList<>();
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET);
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                CartList cartList = new CartList();
                cartList.setUsername(resultSet.getString(2));
                cartList.setItemid(resultSet.getString(3));
                cartList.setProductid(resultSet.getString(4));
                cartList.setDescription(resultSet.getString(5));
                cartList.setQuantity(resultSet.getInt(6));
                cartList.setInStock(resultSet.getString(7));
                cartList.setTotal(resultSet.getBigDecimal(8));
                cartLists.add(cartList);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return cartLists;
    }
}
